package cn.edu.nuc.weiboclient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import oauth.signpost.OAuth;
import android.app.ListActivity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.AbsListView.OnScrollListener;
import cn.edu.nuc.wangchao.modle.*;
import com.google.gson.Gson;

public class FriendsTimeLineActivity extends ListActivity{
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuItem followsItem = menu.add(Menu.NONE, 1, 1, "Home");
		MenuItem mainItem = menu.add(Menu.NONE, 0, 0, "ME");
		MenuItem friendsItem = menu.add(Menu.NONE, 2, 2, "Funs");
		MenuItem homepageItem = menu.add(Menu.NONE, 3, 3, "@Me");
		MenuItem otherUsersItem = menu.add(Menu.NONE,4, 4, "LoginInterface");
		MenuItem usermentionedItem = menu.add(Menu.NONE, 5, 5, "Exit");
		
		followsItem.setIcon(android.R.drawable.ic_menu_send);
		friendsItem.setIcon(android.R.drawable.ic_menu_more);
		homepageItem.setIcon(android.R.drawable.ic_menu_send);
		otherUsersItem.setIcon(android.R.drawable.ic_menu_more);
		usermentionedItem.setIcon(android.R.drawable.ic_menu_send);
		mainItem.setIcon(android.R.drawable.ic_media_rew);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if(item.getItemId() == 5){
			finish();
			System.exit(0);
		}
		else if(item.getItemId() == 1){
			Intent intent = new Intent();
			intent.setClass(this,HomepageTimeLine.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 2){
			Intent intent = new Intent();
			intent.setClass(this, FollowsTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 3){
			Intent intent = new Intent();
			intent.setClass(this,UsermentionedTimeLine.class);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 4){
			Intent intent = new Intent();
			intent.setClass(this,WeiboClientActivity.class).setFlags(intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			finish();
		}
		else if(item.getItemId() == 0){
			Intent intent = new Intent();
			intent.setClass(this,MyPostTimeLine.class);
			startActivity(intent);
			finish();
		}
		return super.onMenuItemSelected(featureId, item);
	}
	private ListView listView = null;
	private List<FollowsInfo> followsInfos = null;
	private RelationListAdapter adapter = null;
	private SharedPreferences prefs = null;
	private boolean isUpdating = false;
	private int startindex = 0;
	private int page = 0;
	private ImageButton enter = null;
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.followlist);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		enter = (ImageButton)findViewById(R.id.enter);
		enter.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
            	intent.setClass(FriendsTimeLineActivity.this,Enter.class);
            	startActivity(intent);
            	finish();
				
			}
		});
		followsInfos = getWeiBoDatasByStartindex(startindex);
		adapter = new RelationListAdapter(followsInfos, this);
		listView = getListView();
		listView.setAdapter(adapter);
		listView.setOnScrollListener(new OnScrollListener() {
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				
			}
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if(firstVisibleItem + visibleItemCount == totalItemCount && !isUpdating){
					AsyncUpdateWeiBoDatasTask asyncUpdateWeiBoDatasTask = new AsyncUpdateWeiBoDatasTask();
					asyncUpdateWeiBoDatasTask.execute();
					System.out.println("begin update-------------");
				}
			}
		});
	}
	private List<FollowsInfo> getWeiBoDatasByStartindex(long startindex) {
		Map<String, String> keyValues = new HashMap<String, String>();
		keyValues.put("format", "json");
		keyValues.put("reqnum", "10");
		keyValues.put("startindex", startindex + "");
		
		WeiBoClient weiBoClient = new WeiBoClient(Constants.CONSUMER_KEY,
				Constants.CONSUMER_SECRET, prefs.getString(OAuth.OAUTH_TOKEN,
						""), prefs.getString(OAuth.OAUTH_TOKEN_SECRET, ""));
		String result = weiBoClient.doGet(Constants.WeiBoApi.FRI,
				keyValues);
		System.out.println("result------->" + result);
		
		Gson gson = new Gson();
		FollowsList followsList = gson.fromJson(result, FollowsList.class);
		List<FollowsInfo> followsinfo = followsList.getData().getInfo();
		int hasnext = followsList.getData().getHasnext();
		
		if(hasnext == 0){
			isUpdating = false;
		}
		else if(hasnext == 1){
			isUpdating = true;
		}
		this.startindex = 10*(++page);
		return followsinfo;
	}
	class AsyncUpdateWeiBoDatasTask extends AsyncTask<Void, Void, List<FollowsInfo>>{

		@Override
		protected List<FollowsInfo> doInBackground(Void... params) {
			isUpdating = true;
			return getWeiBoDatasByStartindex(startindex);
		}
		@Override
		protected void onPostExecute(List<FollowsInfo> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			followsInfos.addAll(result);
			adapter.notifyDataSetChanged();
			//isUpdating = false;
			System.out.println("end update--------------");
		}	
	}
}